package com.tanhua.server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapLikeType;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import com.tanhua.server.pojo.Question;
import com.tanhua.server.pojo.UserQuestion;
import com.tanhua.server.service.TestLinHunService;
import com.tanhua.server.vo.LinHunVo;

import com.tanhua.server.vo.RportVo;
import com.tanhua.server.vo.TodayBest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Author:Mr.yuan
 * @Description:
 * @Date: Created in 20:57 2021/1/16
 * @ModifiedBy:
 */

@RequestMapping("testSoul")
@RestController
@Slf4j
public class TestLinHunController {

    @Autowired
    private TestLinHunService testLinHunService;

    @GetMapping
    public Object[] queryTestSoul() {

        List<LinHunVo> linHunVos = this.testLinHunService.queryTestSoul();

        System.out.println(linHunVos);
        return linHunVos.toArray();
    }

    @GetMapping("report/{id}")
    public ResponseEntity<RportVo> queryReport( @PathVariable("id") String id) {

            RportVo reportVos= this.testLinHunService.queryReport(id);

        return ResponseEntity.ok(reportVos);
    }

    @PostMapping
    public ResponseEntity<String> TestSoul(@RequestBody String answers) throws IOException {
        ArrayList<UserQuestion> list = new ArrayList<>();
        Map map = JSON.parseObject(answers, Map.class);
        List answersList = (List) map.get("answers");
        for (Object o : answersList) {
            String s = o.toString();
            UserQuestion userQuestion = JSON.parseObject(s, UserQuestion.class);
            list.add(userQuestion);
        }

        try {

            String id= this.testLinHunService.testSoul(list);
            //调用业务层方法进行查询
            if (null !=id ) {
                return ResponseEntity.ok(id);
            }
        } catch (Exception e) {
            log.error("灵魂错误~ ", e);
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }


}



